package com.youdianda.controller.ydd;

import com.youdianda.dto.PageQueryDTO;
import com.youdianda.dto.YddLinkPageDTO;
import com.youdianda.dto.YddUserFavoriteLikesArticleDTO;
import com.youdianda.entity.YddArticle;
import com.youdianda.entity.YddUser;
import com.youdianda.result.PageResult;
import com.youdianda.result.Result;
import com.youdianda.service.YddArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


/**
 * 前台接口/6.2.用户相关
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private YddArticleService yddArticleService;

    /**
     * 我的文章
     * @param dto
     * @return
     */
    @GetMapping("/myArticle")
    public Result<PageResult> myArticle(PageQueryDTO dto){
        Result<PageResult> result = yddArticleService.myArticle(dto);
        return result;
    }

    /**
     * 我的收藏/点赞文章
     * @param dto
     * @return
     */
    @GetMapping("/userDataList")
    public Result userDataList(YddLinkPageDTO dto){
        return yddArticleService.userDataList(dto);
    }

    /**
     * 修改文章
     * @param yddArticle
     * @return
     */
    @PostMapping("/editArticle")
    @Transactional
    public Result editArticle(@RequestBody YddArticle yddArticle){
        yddArticleService.editArticle(yddArticle);
        return Result.success();
    }

    /**
     * 修改用户资料
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    @Transactional
    public Result update(@RequestBody YddUser yddUser){
        yddArticleService.updateUserInfo(yddUser);
        return Result.success();
    }

    /**
     * 点赞/取消点赞和收藏/取消收藏
     * @param dto
     * @return
     */
    @GetMapping("/userDataHandle")
    public Result userDataHandle(YddUserFavoriteLikesArticleDTO dto){
        yddArticleService.userDataHandle(dto);
        return Result.success();
    }

    /**
     * 发布文章
     * @param yddArticle
     * @return
     */
    @PostMapping("/addArticle")
    @Transactional
    public Result addArticle(@RequestBody YddArticle yddArticle){
        yddArticleService.addArticle(yddArticle);
        return Result.success();
    }

    /**
     * 删除我的文章
     * @param id
     * @return
     */
    @GetMapping("/delArticle")
    public Result delArticle(String id){
        yddArticleService.removeById(id);
        return Result.success();
    }
}
